﻿using Core.Event;
using Core.File;
using UnityEngine;

namespace Game {
    /// <summary>
    /// 玩家装备模块
    /// </summary>
    public class PlayerEquipModule : PlayerModule {
        //当前武器
        private Weapon mCurWeapon;
        public int CurIndex;
        
        public PlayerEquipModule(Player player) : base(player) { }

        public override void Init() {
            GameEventMgr.Ins.RegisterEvent(GameEventID.TriggerWeapon,onUseWeapon);
            UseWeapon(0);
        }
        public override void Release() {
            GameEventMgr.Ins.UnRegisterEvent(GameEventID.TriggerWeapon,onUseWeapon);
        }
        public Weapon GetWeapon() {
            return mCurWeapon;
        }
        
        public void UseWeapon(int index) {
            CurIndex = index;
            mCurWeapon = mPlayer.BagModule.WeaponBag.GetWeapon(index);
            if (mCurWeapon == null) {
                return;
            }
            mCurWeapon.GameInstance.transform.parent = mPlayer.Component.Hand;
            mCurWeapon.GameInstance.transform.localPosition = Vector3.zero;
        }

        private void onUseWeapon(GameEvent evt) {
            if (mCurWeapon != null) {
                mCurWeapon.Trigger();
            }
        }

        private void onUseWeaponSkill(GameEvent evt) {
            if (mCurWeapon != null) {
                mCurWeapon.SkillTrigger();
            }
        }
    }
}